# -*- coding: utf-8 -*-

from osv import fields,osv
from osv.orm import browse_record
import tools
from functools import partial
import pytz
import pooler
from tools.translate import _
from service import security
import netsvc
import datetime

def get_readonly(user, course):
    res = True
    if course.lec_id.id != user.id:
        res = True
    else:
        for profile in user.profiles_ids:
            if profile.name in (u'Giảng viên',):
                res = False
                break
#    if not res:
#        if course:
#            if course.mark_update_done:
#                res = True
    return res

def get_invisible(user, course):
    res = True
    if course.lec_id.id != user.id:
        res = False
    else:
        for profile in user.profiles_ids:
            if profile.name in (u'Giảng viên',):
                res = False
                break
#    if not res:
#        if course:
#            if course.mark_update_done:
#                res = True
    return res

class aas_course_result(osv.TransientModel):
    _name = 'aas.course.result'
    _columns = {
        'results_ids': fields.many2many('aas.course.student', 'result_student', 'course_result_id', 'course_student_id', string=''),
        'readonly': fields.boolean('', readonly=True),
        'invisible': fields.boolean('', readonly=True),
    }
    _defaults = {  
        'readonly': lambda *a: True,
    }
    
    def default_get(self, cr, uid, fields, context=False):
        res = super(aas_course_result, self).default_get(cr, uid, fields, context=context)
        id = context.get('active_id', False)
        self.course_id = False
        self.course = False
        if id:
            course_obj = self.pool.get('aas.course.course')
            course = course_obj.browse(cr, uid, id, context=context)
            self.course = course
            ids = []
            for result in course.course_students:
                ids.append(result.id)
            res.update({'results_ids': ids})
            
            if ids:
                sql = '''
                    SELECT course_id
                    FROM aas_course_student
                    WHERE id = %s
                '''
                cr.execute(sql, (ids[0],))
                if cr.rowcount > 0:
                    self.course_id = cr.fetchone()[0]
        user_obj = self.pool.get('res.users')
        user = user_obj.browse(cr, uid, uid, context=context)
        res.update({'readonly': get_readonly(user, self.course), 'invisible': get_invisible(user, self.course)})
        return res
    
    def print_report(self, cr, uid, ids, context=False):
        datas = {}
        if not self.course_id:
            raise osv.except_osv(_('Thông báo!'), _("Kết quả trống, không thể tải bản in!"))
        datas['id'] = self.course_id
        datas['ids'] = [self.course_id]
        datas['model'] = 'aas.course.course'
        return {
            'type': 'ir.actions.report.xml', 
            'report_name': 'aas_course_result_report', 
            'datas': datas
        }
    
#    def fields_view_get(self, cr, uid, view_id=None, view_type='form', context=None, toolbar=False, submenu=False):
#        id = context.get('active_id', False)
#        if id:
#            course_obj = self.pool.get('aas.course.course')
#            course = course_obj.browse(cr, uid, id, context=context)
#            if not course.mark_update_done:
#                user_obj = self.pool.get('res.users')
#                user = user_obj.browse(cr, uid, uid, context=context)
#                res = False
#                for profile in user.profiles_ids:
#                    if profile.name in (u'Giảng viên',):
#                        res = True
#                        break
#                if not res:
#                    year_obj = self.pool.get('aas.academic.year')
#                    view_id = year_obj.get_view_id(cr, uid, 'aas_course', 'aas_course_result_form_error', context=context)
#        result = super(aas_course_result, self).fields_view_get(cr, uid, view_id=view_id, view_type=view_type, context=context, toolbar=toolbar, submenu=submenu)
#        return result
aas_course_result()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
